home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1993…ch: Other People's Memory / ADC Developer CD (1993-03) (''Other People's Memory'')_iso / Dev.CD Mar 93.iso / Technical Documentation / Sample Code / DTS.Lib & Samples / DTS.Lib / AppleTalk.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-22  |  9.1 KB  |  340 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** Program:         DTS.Lib
  5. ** File:         AppleTalk.c
  6. ** Written by:   Pete Helme and Jim Luther
  7. ** Modified by:  Eric Soldan
  8. **
  9. ** Copyright © 1989-1991 Apple Computer, Inc.
  10. ** All rights reserved.
  11. */
  12.  
  13.  
  14. /*****************************************************************************/
  15.  
  16.  
  17.  
  18. #include "DTS.Lib2.h"
  19. #include "DTS.Lib.protos.h"
  20.  
  21. #ifndef __ERRORS__
  22. #include <Errors.h>
  23. #endif
  24.  
  25. #ifndef __LISTCONTROL__
  26. #include "ListControl.h"
  27. #endif
  28.  
  29. #ifndef __OSUTILS__
  30. #include <OSUtils.h>
  31. #endif
  32.  
  33. #ifndef __RESOURCES__
  34. #include <Resources.h>
  35. #endif
  36.  
  37. #include "Utilities.h"
  38.  
  39.  
  40.  
  41. #define        kATPTimeOutVal            3            /* re-try ATP SendRequest every 3 seconds */
  42. #define        kATPRetryCount            5            /* for five times */
  43. #define        kZonesSize                578            /* size of buffer for zone names */
  44. #define        kGetMyZoneCall            0x07000000    /* GetMyZone indicator */
  45. #define        kGetZoneListCall        0x08000000    /* GetZoneList indicator */
  46. #define        kZIPSocket                6            /* the Zone Information Protocol socket */
  47. #define        kMoreZones                0xFF000000     /* mask to see if more zones to come */
  48. #define        kZoneCount                0x0000FFFF     /* mask to count zones in buffer */
  49.  
  50. #define        kTupleSize    104
  51. #define        kMaxTuples    1
  52.  
  53.  
  54.  
  55. /*****************************************************************************/
  56.  
  57.  
  58.  
  59. #pragma segment myPPC
  60. OSErr    ATInit(void)
  61. {
  62.     return(noErr);
  63. }
  64.  
  65.  
  66.  
  67. /*****************************************************************************/
  68.  
  69.  
  70.  
  71. /* Create the list of zones on the network.  Find a bridge to talk to, if one is
  72. ** present, then ask it for zone names.  Add the names to the passed-in list. */
  73.  
  74. #pragma segment myPPC
  75. OSErr    DoBuildZoneList(ListHandle listHndl)
  76. {
  77.     ATPParamBlock    atppb;
  78.     char            zones[kZonesSize], *zptr, data[255];
  79.     OSErr            err;
  80.  
  81.     BDSElement    dBDS;                /* the BDS for GetZoneList call */
  82.     short        index, count, i;
  83.     short        ignore;
  84.     short        nodeNetAddress, bridgeNode;
  85.  
  86.     dBDS.buffSize = kZonesSize;                                    /* set up BDS */
  87.     dBDS.buffPtr = zones;
  88.  
  89.     atppb.ATPatpFlags = 0;
  90.  
  91.     /* Get network address of node & node ID of bridge (if any). */
  92.  
  93.     err = GetNodeAddress(&ignore, &nodeNetAddress);
  94.     if (err) return(err);
  95.  
  96.     if (!(bridgeNode = GetBridgeAddress())) return(noErr);
  97.         /* We have added all zero zones to the ist, so we are done. */
  98.  
  99.     atppb.ATPaddrBlock.aNet = nodeNetAddress;
  100.     atppb.ATPaddrBlock.aNode = bridgeNode;            /* Get node of bridge. */
  101.     atppb.ATPaddrBlock.aSocket = kZIPSocket;        /* The socket we want. */
  102.     atppb.ATPreqLength = 0;
  103.     atppb.ATPreqPointer = nil;
  104.     atppb.ATPbdsPointer = (Ptr) &dBDS;
  105.     atppb.ATPnumOfBuffs = 1;
  106.     atppb.ATPtimeOutVal = kATPTimeOutVal;
  107.     atppb.ATPretryCount = kATPRetryCount;
  108.  
  109.     index = 1;
  110.     count = 0;
  111.  
  112.     do {
  113.         atppb.ATPuserData = kGetZoneListCall + index;    /* Indicate GetZoneList request. */
  114.         err = PSendRequest(&atppb, false);                /* Send sync request. */
  115.         if (err) return(err);
  116.  
  117.         count += dBDS.userBytes & kZoneCount;            /* find out how many returned */
  118.         zptr = zones;                                    /* put current pointer at start */
  119.         do {                                            /* get each zone */
  120.             for (i = zptr[0]; i; --i)
  121.                 data[i - 1] = zptr[i];
  122.             CLInsert(listHndl, data, zptr[0], -1, 0);
  123.             zptr += (*zptr + 1 );                        /* bump up current pointer*/
  124.             ++index;                                    /* increment which zone */
  125.         } while(index <= count);
  126.  
  127.     } while ((dBDS.userBytes & kMoreZones) == 0);        /*     keep going until none left */
  128.  
  129.     return(noErr);
  130. }
  131.  
  132.  
  133.  
  134. /*****************************************************************************/
  135.  
  136.  
  137.  
  138. /* Select our zone in the zone list. */
  139.  
  140. #pragma segment myPPC
  141. OSErr    OldStyleGetMyZone(StringPtr str);
  142. OSErr    HiliteUserZone(ListHandle listHndl)
  143. {
  144. //    XPPParamBlock    xcall;
  145.     Str32            zone;
  146.     short            zoneLen, i;
  147.     Point            cell;
  148.     OSErr            err;
  149.  
  150. #if 0
  151.     xcall.XCALL.xppTimeout = 3;
  152.     xcall.XCALL.xppRetry   = 4;
  153.     xcall.XCALL.zipBuffPtr = (Ptr)zone;
  154.     xcall.XCALL.zipInfoField[0] = 0;
  155.     xcall.XCALL.zipInfoField[1] = 0;
  156. #endif
  157.  
  158. //    if (!(err = GetMyZone(&xcall, false))) {
  159.  
  160.     if (!(err = OldStyleGetMyZone(zone))) {
  161.         for (zoneLen = zone[i = 0]; i < zoneLen; ++i) zone[i] = zone[i + 1];
  162.         cell.h = cell.v = 0;
  163.         if (LSearch(zone, zoneLen, nil, &cell, listHndl)) {
  164.             LSetSelect(true, cell, listHndl);
  165.             LAutoScroll(listHndl);
  166.         }
  167.     }
  168.     return(err);
  169. }
  170.  
  171.  
  172.  
  173. /*****************************************************************************/
  174.  
  175.  
  176.  
  177. #pragma segment myPPC
  178. OSErr    OldStyleGetMyZone(StringPtr str)
  179. {
  180.     ATPParamBlock    atppb;
  181.     OSErr            err;
  182.  
  183.     BDSElement    dBDS;                /* the BDS for GetZoneList call */
  184.     short        ignore;
  185.     short        nodeNetAddress, bridgeNode;
  186.  
  187.     dBDS.buffSize = sizeof(Str32);
  188.     dBDS.buffPtr  = (Ptr)str;
  189.  
  190.     atppb.ATPatpFlags = 0;
  191.  
  192.     /* Get network address of node & node ID of bridge (if any). */
  193.  
  194.     err = GetNodeAddress(&ignore, &nodeNetAddress);
  195.     if (err) return(err);
  196.  
  197.     if (!(bridgeNode = GetBridgeAddress())) return(noErr);
  198.         /* We have added all zero zones to the ist, so we are done. */
  199.  
  200.     atppb.ATPaddrBlock.aNet    = nodeNetAddress;
  201.     atppb.ATPaddrBlock.aNode   = bridgeNode;            /* Get node of bridge. */
  202.     atppb.ATPaddrBlock.aSocket = kZIPSocket;            /* The socket we want. */
  203.     atppb.ATPreqLength  = 0;
  204.     atppb.ATPreqPointer = nil;
  205.     atppb.ATPbdsPointer = (Ptr) &dBDS;
  206.     atppb.ATPnumOfBuffs = 1;
  207.     atppb.ATPtimeOutVal = kATPTimeOutVal;
  208.     atppb.ATPretryCount = kATPRetryCount;
  209.  
  210.     atppb.ATPuserData = kGetMyZoneCall;                /* Indicate GetMyZone request. */
  211.     return(PSendRequest(&atppb, false));            /* Send sync request. */
  212. }
  213.  
  214.  
  215.  
  216.  
  217. /*****************************************************************************/
  218.  
  219.  
  220.  
  221. /* This routine finds the socket used by the PPC Toolbox (it's the one with
  222. ** type 'PPCToolBox') and gives it a NBP alias (it registers a new NBP name
  223. ** on that socket) with the type passed in newNBPType.  The NameTableEntry
  224. ** record passed to this routine must be allocated globally (or must be a
  225. ** locked block on the heap until RemoveNBPAlias is called).  The variable
  226. ** newEntity is filled in with the new entity name and is returned to the
  227. ** function's caller so it can be passed to the RemoveNBPAlias function
  228. ** (below).  You'll get an error if any call fails, if an entity of type
  229. ** 'PPCToolBox' is not found (usually because either Program Linking isn't
  230. ** enabled or AppleTalk is disabled), or if RegisterName fails because >100
  231. ** copies of your application are running on the one machine. */
  232.  
  233. #pragma segment myPPC
  234. OSErr    AddPPCNBPAlias(NamesTableEntry *theNTE, Str32 newNBPType, EntityName *newEntity)
  235. {
  236.     OSErr            err;
  237.     MPPParamBlock    pb;
  238.     char            keepSelfFlag;
  239.     short            keepResFile, origLen, num, len;
  240.  
  241.     EntityName        myEntityName;
  242.  
  243.     AddrBlock        myAddrBlock;
  244.     char            myRetBuff[kTupleSize];
  245.     Handle            machineNameHndl;
  246.     Str32            machineName;
  247.  
  248.     pb.SETSELF.newSelfFlag = 1;
  249.     err = PSetSelfSend(&pb, false);                        /* Turn on self-send. */
  250.     if (err) return(err);
  251.  
  252.     keepSelfFlag = pb.SETSELF.oldSelfFlag;                /* Keep old self-send flag. */
  253.  
  254.     keepResFile = CurResFile();
  255.     UseResFile(0);
  256.     machineNameHndl = GetResource('STR ', -16413);        /* Get machine name. */
  257.     UseResFile(keepResFile);
  258.  
  259.     if (!machineNameHndl) {
  260.         pb.SETSELF.newSelfFlag = keepSelfFlag;
  261.         PSetSelfSend(&pb, false);
  262.         return(ResError());
  263.     }
  264.  
  265.     pcpy(machineName, (StringPtr)*machineNameHndl);        /* Keep a copy of the machine name.   */
  266.     ReleaseResource(machineNameHndl);                    /* Release the machine name resource. */
  267.  
  268.     NBPSetEntity((Ptr)&myEntityName, (Ptr)machineName, (Ptr)"\pPPCToolBox", (Ptr)"\p*");
  269.  
  270.     pb.NBPinterval    = 1;                    /* We want to build the entity name using */
  271.     pb.NBPcount       = 1;                    /* the machine name and 'PPCToolBox'.     */
  272.     pb.NBPentityPtr   = (Ptr)&myEntityName;
  273.     pb.NBPretBuffPtr  = myRetBuff;
  274.     pb.NBPretBuffSize = (kTupleSize * kMaxTuples);
  275.     pb.NBPmaxToGet    = kMaxTuples;
  276.  
  277.     if (!(err = PLookupName(&pb, false))) {        /* If lookup was okay... */
  278.         if (pb.NBPnumGotten) {                    /* If entity was found... */
  279.                                     /* ...we found the socket used by the PPC Toolbox. */
  280.                                     /* This means that there is a socket, due to program */
  281.                                     /* linking being turned on. */
  282.  
  283.             NBPExtract(myRetBuff, pb.NBPnumGotten, 1, &myEntityName, &myAddrBlock);
  284.                 /* Break the tuple into component parts. */
  285.  
  286.             for (origLen = machineName[num = 0]; num < 100;) {
  287.  
  288.                 pb.NBPinterval   = 7;
  289.                 pb.NBPcount      = 5;
  290.                 pb.NBPentityPtr  = (Ptr)theNTE;
  291.                 pb.NBPverifyFlag = 1;
  292.  
  293.                 NBPSetNTE((Ptr)theNTE, (Ptr)machineName, (Ptr)newNBPType, (Ptr)"\p*", myAddrBlock.aSocket);
  294.                 NBPSetEntity((Ptr)newEntity, (Ptr)machineName,(Ptr) newNBPType, (Ptr)"\p*");
  295.                 err = PRegisterName(&pb, false);
  296.  
  297.                 if (err != nbpDuplicate) break;
  298.                     /* We registered the name, (or badness happened), so we are done. */
  299.  
  300.                 len = 31;        /* The name we tried already exists, so make up an alternate. */
  301.                 if (++num > 9)
  302.                     --len;
  303.                 if (origLen > len)
  304.                     origLen = len;
  305.                 machineName[0] = origLen;
  306.                 pcatdec(machineName, num);
  307.             }
  308.         }
  309.     }
  310.  
  311.     pb.SETSELF.newSelfFlag = keepSelfFlag;
  312.     PSetSelfSend(&pb, false);
  313.  
  314.     return(err);
  315. }
  316.  
  317.  
  318.  
  319.  
  320. /*****************************************************************************/
  321.  
  322.  
  323.  
  324. /* This function removes the entity specified by theEntity from the registered
  325. ** names queue.  You'll get an error if theEntity hasn't been registered on
  326. ** this Macintosh with RegisterName. */
  327.  
  328. #pragma segment myPPC
  329. OSErr    RemoveNBPAlias(EntityPtr theEntity)
  330. {
  331.     MPPParamBlock    pb;
  332.  
  333.     pb.MPPioCompletion = nil;
  334.     pb.NBPentityPtr    = (Ptr)theEntity;
  335.     return(PRemoveName(&pb, false));
  336. }
  337.  
  338.  
  339.  
  340.